package com.city.ajax.house;

import com.city.domain.city.CityHouseinfo;
import com.city.domain.house.CityHouseInfoDetails;
import com.city.domain.pub.PubUser;
import com.city.service.city.CityHouseinfoService;
import com.city.service.city.CityTownService;
import com.city.service.city.CityVillageService;
import com.city.service.pub.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 李顺时
 * @Description: 出租房管理
 * @Date: Create in 16:41 2018/8/20
 * @Modified By:
 * */

@RequestMapping("/ajax/city-house")
@RestController
public class CityHouseInfoAjax {

    @Autowired
    public SaveService saveService;

    @Autowired
    private PubUserService pubUserService;

    @Autowired
    private PubProvinceService provinceService;

    @Autowired
    private PubCityService pubCityService;

    @Autowired
    private PubCountyService pubCountyService;

    @Autowired
    private CityTownService townService;

    @Autowired
    private CityVillageService villageService;

    @Autowired
    private PubNationCategoryService pubNationCategoryService;

    @Autowired
    private PubFamilyRelationshipService relationshipService;

    @Autowired
    private PubIndustryCategoryService industryCategoryService;

    @Autowired
    private PubPoliticalOutlookService outlookService;

    @Autowired
    private CityHouseinfoService cityHouseinfoService;

    @Autowired
    private DeleteService deleteService;


    /**
     * @Author: 李顺时
     * @Description: 获取出租房表中的信息 并用新的类封装数据  转换地址的名称比较耗时
     * @Date: 21:17 2018/8/21
     */
    @PostMapping("/getcityhouseinfo")
    @ResponseBody
    public Map<String,Object> findCityHouseinfoLimit(
            @RequestParam(value = "limitPage") Integer limitPage,
            @RequestParam(value ="limitNum") Integer limitNum)
    {
        Map<String,Object> map = new HashMap<String,Object>();


        List<CityHouseinfo>  cityHouseinfos = cityHouseinfoService.getHouseinfoLimit(limitPage, limitNum);
        List<CityHouseInfoDetails> chifl = new ArrayList<>();
        long startTime = System.currentTimeMillis();
        for (CityHouseinfo c: cityHouseinfos) {
            PubUser user = pubUserService.findById(c.getUserId());
            CityHouseInfoDetails infoDetails = new CityHouseInfoDetails();
            String addr = "四川省宜宾市兴文县"+townService.findTownNameByCode(Integer.parseInt(c.getTown()))+villageService.findVillageNameById(Integer.parseInt(c.getVillage()))+c.getAddress();

            infoDetails.setId(c.getId());
            infoDetails.setVillage(Integer.parseInt(c.getVillage()));
            infoDetails.setHouseType(c.getHouseType());
            infoDetails.setUserId(c.getUserId());
            infoDetails.setUpdateTime(c.getUpdateTime());
            infoDetails.setTown(Integer.parseInt(c.getTown()));
            infoDetails.setSunrise(c.getSunrise());
            infoDetails.setStatus(c.getStatus());
            infoDetails.setProvince(c.getProvince());
            infoDetails.setPrice(Double.parseDouble(c.getPrice().toString()));
            infoDetails.setNumber(c.getNumber());
            infoDetails.setLng(Double.parseDouble(c.getLng().toString()));
            infoDetails.setLat(Double.parseDouble(c.getLat().toString()));
            infoDetails.setAddr(addr);
            infoDetails.setFloorCount(c.getFloorCount());
            infoDetails.setFilenumber(c.getFilenumber());
            infoDetails.setFloor(c.getFloor());
            infoDetails.setFacilities(c.getFacilities());
            infoDetails.setDecorationStatus(c.getDecorationStatus());
            infoDetails.setCounty(c.getCounty());
            infoDetails.setCity(c.getCity());
            infoDetails.setAreaBuild(c.getAreaBuild());
            infoDetails.setName(user.getName());
            infoDetails.setSex(getSex(user.getSex()));
            infoDetails.setTelphone(c.getTellphone());
            chifl.add(infoDetails);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间： "+(endTime-startTime)*1.0/1000+"s");
        map.put("data", chifl);
        map.put("res", "1");
        map.put("count", cityHouseinfoService.count());
        map.put("code", "成功");
        return map;
    }


    /**
     * @Author: 李顺时
     * @Description: 出租房管理-详细  所用到的方法 和上面方法差不多 只是将地址拆分开
     * @Date: 21:19 2018/8/21
     */
    @PostMapping("/getcityhouseinfobyid")
    @ResponseBody
    public Map<String,Object> findCityHouseinfoLimitById(
            @RequestParam(value = "id") Integer id)
    {
        Map<String,Object> map = new HashMap<String,Object>();


        CityHouseinfo  c = cityHouseinfoService.getHouseinfo(id);
        long startTime = System.currentTimeMillis();
        PubUser user = pubUserService.findById(c.getUserId());
        CityHouseInfoDetails infoDetails = new CityHouseInfoDetails();
        String addr = townService.findTownNameByCode(Integer.parseInt(c.getTown()))+villageService.findVillageNameById(Integer.parseInt(c.getVillage()))+c.getAddress();

        infoDetails.setId(c.getId());
        infoDetails.setVillage(Integer.parseInt(c.getVillage()));
        infoDetails.setHouseType(c.getHouseType());
        infoDetails.setUserId(c.getUserId());
        infoDetails.setUpdateTime(c.getUpdateTime());
        infoDetails.setTown(Integer.parseInt(c.getTown()));
        infoDetails.setSunrise(c.getSunrise());
        infoDetails.setStatus(c.getStatus());
        infoDetails.setProvince(provinceService.findNameByCode(c.getProvince()));
        infoDetails.setPrice(Double.parseDouble(c.getPrice().toString()));
        infoDetails.setNumber(c.getNumber());
        infoDetails.setLng(Double.parseDouble(c.getLng().toString()));
        infoDetails.setLat(Double.parseDouble(c.getLat().toString()));
        infoDetails.setAddr(addr);
        infoDetails.setFloorCount(c.getFloorCount());
        infoDetails.setFilenumber(c.getFilenumber());
        infoDetails.setFloor(c.getFloor());
        infoDetails.setFacilities(c.getFacilities());
        infoDetails.setDecorationStatus(c.getDecorationStatus());
        infoDetails.setCounty(pubCountyService.findCountyNameByCode(c.getCounty()));
        infoDetails.setCity(pubCityService.findNameByCode(c.getCity()));
        infoDetails.setAreaBuild(c.getAreaBuild());
        infoDetails.setName(user.getName());
        infoDetails.setSex(getSex(user.getSex()));
        infoDetails.setTelphone(c.getTellphone());


        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间： "+(endTime-startTime)*1.0/1000+"s");
        map.put("data", infoDetails);
        map.put("res", "1");
        map.put("count", 1000);
        map.put("code", "成功");
        return map;
    }


    /**
     * @Author: 李顺时
     * @Description: 出租房管理-修改 和第一个方法相同不过 参数不同
     * @Date: 21:20 2018/8/21
     */
    @PostMapping("/getcityhouseinfobyid2")
    @ResponseBody
    public Map<String,Object> findCityHouseinfoLimitById2(
            @RequestParam(value = "id") Integer id)
    {
        Map<String,Object> map = new HashMap<String,Object>();


        CityHouseinfo  c = cityHouseinfoService.getHouseinfo(id);
        long startTime = System.currentTimeMillis();
        PubUser user = pubUserService.findById(c.getUserId());
        CityHouseInfoDetails infoDetails = new CityHouseInfoDetails();
        String addr = c.getAddress();

        infoDetails.setId(c.getId());
        infoDetails.setVillage(Integer.parseInt(c.getVillage()));
        infoDetails.setHouseType(c.getHouseType());
        infoDetails.setUserId(c.getUserId());
        infoDetails.setUpdateTime(c.getUpdateTime());
        infoDetails.setTown(Integer.parseInt(c.getTown()));
        infoDetails.setSunrise(c.getSunrise());
        infoDetails.setStatus(c.getStatus());
        infoDetails.setProvince(c.getProvince());
        infoDetails.setPrice(Double.parseDouble(c.getPrice().toString()));
        infoDetails.setNumber(c.getNumber());
        infoDetails.setLng(Double.parseDouble(c.getLng().toString()));
        infoDetails.setLat(Double.parseDouble(c.getLat().toString()));
        infoDetails.setAddr(addr);
        infoDetails.setFloorCount(c.getFloorCount());
        infoDetails.setFilenumber(c.getFilenumber());
        infoDetails.setFloor(c.getFloor());
        infoDetails.setFacilities(c.getFacilities());
        infoDetails.setDecorationStatus(c.getDecorationStatus());
        infoDetails.setCounty(c.getCounty());
        infoDetails.setCity(c.getCity());
        infoDetails.setAreaBuild(c.getAreaBuild());
        infoDetails.setName(user.getName());
        infoDetails.setSex(getSex(user.getSex()));
        infoDetails.setTelphone(c.getTellphone());


        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间： "+(endTime-startTime)*1.0/1000+"s");
        map.put("data", infoDetails);
        map.put("res", "1");
        map.put("count", 1000);
        map.put("code", "成功");
        return map;
    }

    @PostMapping("/cityhouseinfo-delete")
    @Transactional
    @ResponseBody
    public Map<String,String> deleteCityHouseinfoDelete(@RequestBody CityHouseinfo cityHouseinfo){
        System.out.println(cityHouseinfo);
        Map<String,String> map = new HashMap<String, String>();
        if(cityHouseinfo.getId() <= 0){
            map.put("res", "0");
            map.put("code","操作失败");
        }else {
            deleteService.delete(cityHouseinfo);
            map.put("res", "1");
            map.put("code","操作已完成");
        }
        return map;
    }


    public String getSex(Integer type){
        if(1 == type){
            return  "女";
        }else{
            return "男";
        }
    }
}
